package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Mylinja 2021-12-16 星期四 21:47
 */
@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {

    @Autowired
    private SetmealDishService setmealDishService;

    /**
     * 新增套餐方法
     *
     * @param setmealDto dto类，封装套餐信息
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveWithDish(SetmealDto setmealDto) {
        //A. 保存套餐基本信息
        this.save(setmealDto);

        //B. 获取套餐关联的菜品集合，并为集合中的每一个元素赋值套餐ID(setmealId)
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes = setmealDishes.stream().peek(dish ->
                dish.setSetmealId(setmealDto.getId())
        ).collect(Collectors.toList());

        //C. 批量保存套餐关联的菜品集合
        setmealDishService.saveBatch(setmealDishes);
    }

    /**
     * 删除套餐方法
     *
     * @param ids 需要被删除的套餐id集合
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void removeWithDish(List<Long> ids) {
        //A. 查询该批次套餐中是否存在售卖中的套餐, 如果存在, 不允许删除
        // SQL: select count(*) from setmeal where id in(1,2,3,4...) and status = 1;
        // 这里的这个query条件查询器是为了去查找的
        LambdaQueryWrapper<Setmeal> query = new LambdaQueryWrapper<>();
        query.in(Setmeal::getId, ids);
        query.eq(Setmeal::getStatus, 1);
        // 开始查询
        int count = this.count(query);
        if (count > 0) {
            // 不能删除
            throw new CustomException("套餐正在售卖中，不能删除...");
        }

        //B. 删除套餐数据
        this.removeByIds(ids);

        //C. 删除套餐关联的菜品数据
        // SQl: delete from setmeal_dish where setmeal_id in(1,2,3,4...)
        LambdaQueryWrapper<SetmealDish> queryDish = new LambdaQueryWrapper<>();
        queryDish.in(SetmealDish::getSetmealId, ids);
        // 这里的这个queryDish条件查询器是为了去删除的，删除setmeal_dish表中的关联信息
        setmealDishService.remove(queryDish);
    }

    /**
     * 回显菜品信息
     *
     * @param id 根据id回显
     * @return 返回对象
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public SetmealDto getByIdWithDish(long id) {
        //1 先查出套餐
        Setmeal setmeal = this.getById(id);
        SetmealDto setmealDto = new SetmealDto();
        BeanUtils.copyProperties(setmeal, setmealDto);

        //2 根据套餐的id，在套餐菜品setmeal_dish中间表，查询改套餐的所有菜品
        LambdaQueryWrapper<SetmealDish> query = new LambdaQueryWrapper<>();
        query.eq(SetmealDish::getSetmealId, id);
        List<SetmealDish> list = setmealDishService.list(query);
        setmealDto.setSetmealDishes(list);

        return setmealDto;

    }

    /**
     * 修改套餐方法
     *
     * @param setmealDto 套餐dto对象
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateByIdWithDish(SetmealDto setmealDto) {
        //1 先更新套餐setmeal表
        this.updateById(setmealDto);

        //2 删除setmeal_dish表中的所有菜品信息
        LambdaQueryWrapper<SetmealDish> query = new LambdaQueryWrapper<>();
        query.eq(SetmealDish::getSetmealId, setmealDto.getId());
        setmealDishService.remove(query);

        //3 然后拿到setmealDto中的setmealDishes集合,重新保存到setmeal_dish表中
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        // 断点调试发现，setmealDishes集合中的每个SetmealDish对象，没有setmealId值。需要手动赋值
        setmealDishes = setmealDishes.stream().peek((item) ->
                item.setSetmealId(setmealDto.getId())
        ).collect(Collectors.toList());

        setmealDishService.saveBatch(setmealDishes);
    }
}
